<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=plugin('jy_merch')->checklogin();
$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
$return['plugin_icon']=$merch['logo'];
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','worker_id','title','status','image','remark','price','work_time','serve_time','times','cancel_time','isshow','paixu','createtime');
	$list=pdo_getslice('jy_yuyue_serve',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_yuyue/sever/sever_detail/id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
			array('op'=>'edit','title'=>'添加项目'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：员工可以通过手机，添加属于自己的项目！','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			//array('name'=>'worker_id','title'=>'所属员工','type'=>'select','options'=>'workers'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'isshow','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'项目名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'是否启用','type'=>'radio','options'=>'status'),
		)
	);
	$return['workers']=pdo_getall('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yuyue_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['time_type']=$item['time_type']?'1':'0';
	$item['work_timedays']=$item['work_timedays']?json_decode($item['work_timedays'],true):array();
	$return['item']=$item;
	$return['item']['serve_time']=$return['item']['serve_time'] ? json_decode($return['item']['serve_time'],true):array();
	$return['item']['package']=json_decode($return['item']['package']);
	$return['item']['time_box']=$return['item']['time_box']?json_decode($return['item']['time_box'],true):array();
	$return['item']['fileds']=json_decode($return['item']['fileds']);
	$return['item']['hdps']=$return['item']['hdps']?json_decode($return['item']['hdps'],true):array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'项目名称','name'=>'title','help'=>''),
								array('type'=>'image','title'=>'项目图片','name'=>'image','help'=>''),
								array('type'=>'string','title'=>'项目价格','name'=>'price','help'=>''),
								array('type'=>'unitstring','title'=>'服务人次','name'=>'usernum','unit'=>'人','help'=>''),
								array('type'=>'unitstring','title'=>'项目评分','name'=>'fenshu','unit'=>'分','help'=>'例如：4.5'),
								array('type'=>'unitstring','title'=>'服务时长','name'=>'times','unit'=>$this->setting['timesunit'],'help'=>'可以移步：设置--自定义服务时间单位'),
								array('type'=>'unitstring','title'=>'预约需提前天数','name'=>'advancedays','unit'=>'天'),
								array('type'=>'switch','title'=>'开启客户预约次数限制','name'=>'xianzhi'),
								array('type'=>'unitstring','title'=>'每人可预约总次数','name'=>'alltimes','unit'=>'次','show'=>array('xianzhi'=>1)),
								array('type'=>'unitstring','title'=>'每人每天可预约次数','name'=>'daytimes','unit'=>'次','show'=>array('xianzhi'=>1)),
								array('type'=>'string','title'=>'可取消时间范围','name'=>'cancel_time','help'=>'仅填数字，距离服务时间在该小时之前可选择取消，单位:小时'),
								//array('type'=>'choose','title'=>'指定项目管理员','name'=>'managerid','action'=>'core/member.get_member/','choose_title'=>'选择项目管理员','help'=>'管理员可查看所有该项目订单'),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'首页展示','name'=>'isshow','help'=>'设置是否首页展示'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'richtext','title'=>'项目内容','name'=>'content','help'=>'比如：地址，电话，联系人，服务价格说明等！内容不宜太长！'),
								array('type'=>'text','title'=>'项目描述','name'=>'remark','help'=>''),
								array('type'=>'text','title'=>'注意事项','name'=>'notice','help'=>''),
								array('type'=>'radio','title'=>'服务时间类型','name'=>'time_type','options'=>'time_types','help'=>''),
								array('type'=>'checkbox','title'=>'服务时间','name'=>'work_time','help'=>'','options'=>'work_time','show'=>array('time_type'=>0)),
								array(
								'type'=>'array',
								'name'=>'work_timedays',
								'show'=>array('time_type'=>1),
								'add_tip'=>'服务日期',
								'add_tip'=>'添加一个时间',
								'list'=>array(
									array('type'=>'string','name'=>'day','title'=>'日期'),
								),
								'details'=>array(
									array('type'=>'time','name'=>'day','title'=>'日期'),	
								)
								),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'服务时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',
									'title2'=>'预约',
									'field1'=>'times',
									'field2'=>'yuyue',
									'field2unit'=>'人',
								),
							)
						),
						array(
							'title'=>'项目套餐设置',
							'list'=>array(
								array('type'=>'array','name'=>'package','add_tip'=>'添加一个预约套餐',
								'list'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'套餐名称'),	
									array('type'=>'string','name'=>'package_price','title'=>'服务金额'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'package_title','title'=>'套餐名称'),	
									array('type'=>'string','name'=>'package_price','title'=>'服务金额'),
								)
								),
							)
						),
						array(
							'title'=>'幻灯片',
							'list'=>array(
										array('type'=>'array','title'=>'幻灯片','name'=>'hdps','add_tip'=>'添加一个幻灯片',
												'list'=>array(
													array('type'=>'string','name'=>'title','title'=>'标题'),
													array('type'=>'image','name'=>'picurl','title'=>'图片'),
													array('type'=>'string','name'=>'url','title'=>'地址'),
												),
												'details'=>array(
													array('type'=>'string','name'=>'title','title'=>'标题'),
													array('type'=>'image','name'=>'picurl','title'=>'图片','help'=>'推荐尺寸：750*360'),
													array('type'=>'link','name'=>'url','from'=>'action','title'=>'地址'),
												)
										),
							)
						),
							array(
							'title'=>'自定义字段',
							'list'=>array(
								array('type'=>'array','name'=>'fileds','add_tip'=>'添加一个字段（温馨提示：已经有用户提交过数据的表单，不允许再编辑和添加新字段！）',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
									array('type'=>'radio','name'=>'ismust','title'=>'必填','options'=>'ismust'),	
									array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'type','option_title'=>'type'),
									array('type'=>'switch','name'=>'ismust','title'=>'必填','help'=>''),
									array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
								),
							)
						),
			
			)
	);
	$return['time_types']=$this->time_types;
	$return['work_time']=array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
	$return['type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	$return['ismust']=array('0'=>'否','1'=>'是');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fileds=$postdata['fileds'];
	if(!is_numeric($postdata['fenshu'])){
		exi('评分必须是数字','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'price'=>trim($postdata['price']),
		'work_time'=>trim($postdata['work_time']),
		'serve_time'=>ijson_encode($postdata['serve_time']),
		'package'=>ijson_encode($postdata['package']),
		'cancel_time'=>intval($postdata['cancel_time']),
		'remark'=>trim($postdata['remark']),
		'content'=>trim($postdata['content']),
		'notice'=>trim($postdata['notice']),
		'hdps'=>ijson_encode($postdata['hdps']),
		'times'=>intval($postdata['times']),
		'managerid'=>intval($postdata['managerid']),
		'advancedays'=>intval($postdata['advancedays']),
		'status'=>intval($postdata['status']),
		'isshow'=>intval($postdata['isshow']),
		'paixu'=>intval($postdata['paixu']),
		'xianzhi'=>intval($postdata['xianzhi']),
		'alltimes'=>intval($postdata['alltimes']),
		'daytimes'=>intval($postdata['daytimes']),
		'usernum'=>intval($postdata['usernum']),
		'fenshu'=>trim($postdata['fenshu']),
		'time_type'=>intval($postdata['time_type']),
		'work_timedays'=>ijson_encode($postdata['work_timedays']),
	);
	$id=intval($postdata['id']);
	if($id){
		$count=pdo_count('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$id));
		$hasdata=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'fileds');
		$data['fileds']=ijson_encode($postdata['fileds']);
		/*if($count==0 || !$hasdata){
			$data['fileds']=ijson_encode($postdata['fileds']);
			pdo_delete('jy_yuyue_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$id));
			foreach ($fileds as $item) {
				$insert                = array();
				$insert['uniacid']     = $_SESSION['uniacid'];
				$insert['serve_id']    =$id;
				$insert['title']       =trim($item['title']);
				$insert['type']        =trim($item['type']);
				$insert['ismust']        =intval($item['ismust']);
				$insert['paixu']        =intval($item['paixu']);
				$insert['value']        =trim($item['value']);
				pdo_insert('jy_yuyue_fileds', $insert);
			}
		}*/
		pdo_update('jy_yuyue_serve',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['fileds']=ijson_encode($postdata['fileds']);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		pdo_insert('jy_yuyue_serve',$data);
		$serve_id=pdo_insertid();
		/*foreach ($fileds as $item) {
			$insert                = array();
			$insert['uniacid']     = $_SESSION['uniacid'];
			$insert['serve_id']    =$serve_id;
			$insert['title']       =trim($item['title']);
			$insert['type']        =trim($item['type']);
			$insert['ismust']       =intval($item['ismust']);
			$insert['paixu']       =intval($item['paixu']);
			$insert['value']       =trim($item['value']);
			pdo_insert('jy_yuyue_fileds', $insert);
		}*/

	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yuyue_serve',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_yuyue_serve',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_yuyue_serve',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
